package com.huodongrili.calender;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.huodongrili.calender.bg.ScheduleService;
import com.huodongrili.calender.data.DataPreferences;
import com.huodongrili.calender.network.FeedbackOper;

public class SettingFragment extends Fragment {

	private DataPreferences mDataPre;
	private CheckBox mWifiCb;
	private Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mContext = getActivity();
		
		mDataPre = DataPreferences.getInstance(mContext.getApplicationContext());
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = LayoutInflater.from(getActivity()).inflate(
				R.layout.fragment_setting, container, false);

		mWifiCb = (CheckBox) v.findViewById(R.id.cb_wifi);

		TextView tv = (TextView) v.findViewById(R.id.about_title);
		tv.setText("@" + getResources().getString(R.string.about_title));

		ImageView iSend = (ImageView) v.findViewById(R.id.send_btn);
		final EditText iContent = (EditText) v.findViewById(R.id.send_content);

		RelativeLayout wifiPane = (RelativeLayout) v
				.findViewById(R.id.wifi_pane);
		RelativeLayout aboutPane = (RelativeLayout) v
				.findViewById(R.id.about_pane);
		final CheckBox mAboutNav = (CheckBox) v.findViewById(R.id.cb_exp);
		final ScrollView mAboutSloor = (ScrollView) v
				.findViewById(R.id.about_ds);

		mWifiCb.setChecked(mDataPre.isWifiSync());

		aboutPane.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// showAboutDlg();
				boolean old = mAboutNav.isChecked();
				mAboutNav.setChecked(!old);

				if (!old) {
					mAboutSloor.setVisibility(View.VISIBLE);
				} else {
					mAboutSloor.setVisibility(View.GONE);
				}

			}
		});

		wifiPane.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setWifiSync();
			}
		});

		iSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String content = iContent.getText().toString();
				if (content == null || content.length() <= 0) {
					return;
				}

				/*
				 * FeedbackData fdata = new
				 * FeedbackData(getApplicationContext());
				 * fdata.addFeedback(content, System.currentTimeMillis());
				 */

				new SendTask().execute(content);

				iContent.setText("");

				((InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE))
						.hideSoftInputFromWindow(getActivity().getCurrentFocus()
								.getWindowToken(),
								InputMethodManager.HIDE_NOT_ALWAYS);

				Toast.makeText(mContext, R.string.send_ok,
						Toast.LENGTH_SHORT).show();

			}
		});

		return v;
	}

	private void setWifiSync() {
		boolean wifiStaus = mWifiCb.isChecked();
		boolean nowStatus = !wifiStaus;
		mDataPre.setWifiSync(nowStatus);
		mWifiCb.setChecked(nowStatus);

		Intent i = new Intent(mContext, ScheduleService.class);

		if (nowStatus) {
			mContext.startService(i);
		} else {
			mContext.stopService(i);
		}

	}


	private class SendTask extends AsyncTask<String, Void, Void> {

		@Override
		protected Void doInBackground(String... params) {

			String feedStr = params[0];

			FeedbackOper feedback = new FeedbackOper(mContext.getApplicationContext());
			feedback.toOperFeedback(feedStr);

			return null;
		}
	}

}
